<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link rel="stylesheet" type="text/css" href="../css/mui.css"/>
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="../css/general.css"/>
		<style type="text/css">
			.simg{
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 0.8em;
				background-color: white;
			}
			.simg>img{
				width: 85px;
				height: 85px;
				border-radius: 50%;
			}
			.stext{
				width: 100%;
				background-color: white;
				border-top: 1px solid #c7c7c7;
				padding-left: 10px;
				margin-bottom: 0.4em;
			}
			.border-bot{
				border-bottom: 1px solid #CCCCCC;
			}
			.saddess{
				background-color: white;
				padding-left: 10px;
				margin-bottom: 0.4em;
			}
			.sbz{
				background-color: white;
				padding: 10px;
			}
			textarea{
				word-break: break-word;
			}
			.mui-btn-block {
			    margin-bottom: 0px;
			}
			
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav hbg">
		    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left colorw"></a>
		    <h1 class="mui-title">门店管理</h1>
		</header>
		<div class="mui-content">
		    <div class="simg">
		    	<img id="store_images" src="../images/user-photo.png"/>
		    </div>
		    <div class="stext">
		    	<div class="mui-input-row border-bot">
		    	    <label>门店名称</label>
		    	    <input id="store_name" type="text" class="textr" placeholder="输入昵称">
		    	</div>
		    	<div class="mui-input-row border-bot">
		    	    <label>联系人</label>
		    	    <input id="linkman" type="text" class="textr" placeholder="输入联系人姓名">
		    	</div>
		    	<div class="mui-input-row">
		    	    <label>联系方式</label>
		    	    <input id="phone" type="number" class="textr" placeholder="输入手机号码">
		    	</div>
		    </div>
		    
		    <div class="saddess">
		    	<div class="mui-input-row ">
		    	    <label>地址</label>
		    	    <input id="address" type="text" class="textr" placeholder="输入详细地址">
		    	</div>
		    </div>
		    
		    <div class="sbz">
		    	<textarea id="introduct" name="" rows="6" cols="" placeholder="填写门店介绍"></textarea>
		    </div>
		    <div class="fgd"></div>
		    <div class="footdiv">
		    	<button id="addbut" type="button" class="mui-btn mui-btn-blue mui-btn-block">确定</button>
		    </div>
		    
		</div>
		<script src="../js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="../js/mui.min.js"></script>
		<script src="../js/req.js" type="text/javascript" charset="utf-8"></script>
		<script src="../js/muishow.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			var store_imagesid = 0,
			imgname = "",
			images_id = 0;
			window.onload = function(){
				mui.init();
				getstore();
				
				mui.plusReady(function(){
					g("store_images").addEventListener("tap",function(e){
						page.imgUp();    
					
					})
					var new_back = mui.back;
					mui.back = function() {
					    // 获取当前窗口对象
					    var aPage = plus.webview.currentWebview();
					    //获取父页面对象
					    var bPage = aPage.opener(); 
					    //执行父页面函数
					    bPage.evalJS('onshow()');
					    //关闭
					    new_back();
					};
				})
				g("addbut").addEventListener('tap',function(e){
					var store_name = g("store_name").value,
					linkman = g("linkman").value,
					phone = g("phone").value,
					address = g("address").value,
					introduct = g("introduct").value;
					if(store_name==""){
						mui.toast("门店名称不能为空");
						return;
					}
					if(linkman==""){
						mui.toast("联系人不能为空");
						return;
					}
					if(!phone_validation(phone)){
						mui.toast("联系方式不能为空");
						return;
					}
					if(address==""){
						mui.toast("地址不能为空");
						return;
					}
					if(introduct==""){
						mui.toast("门店介绍不能为空");
						return;
					}
					var data = {
						url:"/api/store/edit",
						data:{
							access_token:acctoken(),
							store_name:store_name,
							store_images:store_imagesid,
							linkman:linkman,
							phone:phone,
							address:address,
							introduce:introduct,
							images_id:store_imagesid>0?store_imagesid:""
						}
					}
					ajax(data,function(res){
						console.log("设置门店信息回调",res);
						console.log(res);
						mui.toast(res.msg);
					})
					
				})
				
				
				
			}
			
			
			
			function getstore(){
				var data = {
					url:"/api/store/detail",
					data:{
						access_token:acctoken(),
					}
				}
				ajax(data,function(res){
					console.log(res);
					g("store_name").value =  res.data.store_name;
					g("linkman").value = res.data.linkman;
					g("phone").value = res.data.phone;
					g("address").value = res.data.address;
					g("introduct").value = res.data.introduce;
					if(res.data.images&&res.data.images.path_name){
						g("store_images").src =  res.data.images.path_name;
						store_imagesid = res.data.images.id;
					}
					
				})
				
			}
			
			
			var page=null;  
            page={  
                imgUp:function(){  
                    var m=this;  
                    plus.nativeUI.actionSheet({cancel:"取消",buttons:[  
                        {title:"拍照"},  
                        {title:"从相册中选择"}  
                    ]}, function(e){//1 是拍照  2 从相册中选择  
                        switch(e.index){  
                            case 1:appendByCamera();break;  
                            case 2:appendByGallery();break;  
                        }  
                    });  
                }  
                //摄像头  
            }  
            
             // 拍照添加文件
            function appendByCamera(){
                plus.camera.getCamera().captureImage(function(e){
                    console.log(e);
                    plus.io.resolveLocalFileSystemURL(e, function(entry) { 
                    	console.log('文件',entry);
                    	var imgSrc = entry.toLocalURL() + "?version=" + new Date().getTime(); //拿到图片路径  
                    	console.log(imgSrc);
                    	cutImage(imgSrc);
                    }, function(e) { 
                        mui.toast("读取拍照文件错误：" + e.message); 
                    }); 
    
                });    
            }
            
            // 从相册添加文件
            function appendByGallery() {
            	plus.gallery.pick(function(a) {
            		plus.io.resolveLocalFileSystemURL(a, function(entry) {
            			plus.io.resolveLocalFileSystemURL("_doc/", function(root) {
            				root.getFile("head.png", {}, function(file) {
            					//文件已存在
            					file.remove(function(){
            						console.log("删除成功");
            						entry.copyTo(root, "head.png", function(e) {
            							var e = e.fullPath + "?version=" + new Date().getTime();
            							console.log("e",e);
            							//uploadHead(e); /*上传图片*/
            							cutImage(e);
            							//变更大的图预览的src
            							//目前只有一张图暂且这样处理，后续需要用标准组件实现
            						}, function(e) {
            							console.log("copy img file:" + e.message);
            						});
            					}, function() {
            						console.log("删除" + e.message)
            					});
            				}, function() {
            					//文件不存在
            					entry.copyTo(root, "head.png", function(e) {
            						var path = e.fullPath + "?version=" + new Date().getTime();
            						console.log("path",path);
            						//uploadHead(path); /*上传图片*/
            						cutImage(path);
            					}, function(e) {
            						console.log("copy img file:" + e.message);
            					});
            				});
            			}, function(e) {
            				console.log("get _www folder fail");
            			})
            		}, function(e) {
            			console.log("读取拍照文件错误" + e.message);
            		})
            	}, function(a) {}, {
            		filter: "image"
            	})
            }
            //上传头像图片
            
            //更新封面
                function update_head_img(e) {
                	var imgurl = e.detail.img;
                	getBase64Image(imgurl,function(res){
                		console.log(res.dataURL);
                		console.log(res.dataURL.length);
                	var data = {
                		url:'/api/upload/image_base64',
                		data:{
                			access_token:acctoken(),
                			images:res.dataURL
                		}
                	}
		                ajax(data,function(resp){
		                	console.log( hurl +"/uploads/images/"+ resp.data.name)
		                	//g("store_images").src = hurl +'/uploads/images/'+ resp.data.name;
		                	g("store_images").src = res.dataURL;
		                	store_imagesid = resp.data.images_id;
		                	imgname =  resp.data.name;
		                })
                })
                	
                }
             window.addEventListener("updateHeadImg", update_head_img); //添加自定义事件，其他页面调用
             
            function cutImage(path) {
            	console.log(path);
            		var list = {
            			 path: path,
                         bmtype:1,
                         pageid:"setpersonal",
                         returnfun:"updateHeadImg",
            		}
                    mui.openWindow({
                        url: '../general/cropper.html',
                        id: 'cropper',
                        extras: list,
                        show: {
                            aniShow: 'zoom-fade-in',
                            duration: 800
                        },
                        waiting: {
                            autoShow: true
                        }
                    });
                }
		</script>
	</body>

</html>